var e = getApp(), t = (e.globalData.url, require("../../../utils/util.js"));

Page({
    data: {
        shopName: "南京双拜巷店",
        tips: "33",
        pingjia: "4.22",
        address: "南京市玄武区孝陵卫双拜巷99号",
        wait: "0",
        queueOrderId: "",
        nowIndex: "0",
        isQueue: !0
    },
    onLoad: function(e) {
        var t = this, a = e.shopId, d = e.carId, s = e.serviceId, u = e.serviceType, i = e.service, o = e.formId;
        if (void 0 !== e.selectTime) {
            var r = e.selectTime;
            t.setData({
                carId: d,
                serviceId: s,
                shopId: a,
                serviceType: u,
                service: i,
                selectTime: r,
                formId: o
            });
        } else t.setData({
            carId: d,
            serviceId: s,
            shopId: a,
            serviceType: u,
            service: i,
            selectTime: "",
            formId: o
        });
        t.getQueueState(a, d, s, u), 1 == i ? t.startyuyue(a, d, s, u) : 0 == i && t.startQueue(a, d, s, u);
    },
    getQueueState: function(a, d, s, u) {
        var i = this;
        t.request(e, "wx/cust/getOneQueueByShopId", "POST", {
            shopId: i.data.shopId,
            token: e.globalData.token,
            state: 0
        }, function(e) {
            e.data.state && (1 == e.data.data.state ? i.setData({
                isQueue: !0,
                addTime: e.data.data.timeStart,
                queueOrderId: e.data.data.queueOrderId
            }) : 0 == e.data.data.state ? i.setData({
                isQueue: !0,
                endTime: e.data.data.timeEnd,
                queueOrderId: e.data.data.queueOrderId
            }) : i.setData({
                isQueue: !1,
                queueOrderId: e.data.data.queueOrderId
            }));
        }, function(e) {});
    },
    startyuyue: function(a, d, s, u) {
        var i = this, o = (i.data.service, i.data.formId);
        t.request(e, "wx/cust/insertOneCBO", "POST", {
            token: e.globalData.token,
            shopId: a,
            orderType: u,
            carId: d,
            servicePackageId: s,
            startTime: i.data.selectTime,
            formId: o
        }, function(e) {
            1 == e.state ? i.setData({
                addTime: e.data.timeStart,
                isQueue: !0,
                queueOrderId: e.data.queueOrderId
            }) : i.setData({
                message: e.message
            });
        }, function(e) {
            wx.showToast({
                title: "预约失败",
                icon: "none"
            });
        });
    },
    startQueue: function(a, d, s, u) {
        var i = this, o = (i.data.service, i.data.formId);
        t.request(e, "wx/cust/addqo", "POST", {
            token: e.globalData.token,
            shopId: a,
            orderType: u,
            carId: d,
            servicePackageId: s,
            formId: o,
            address: e.globalData.address,
            latitude: e.globalData.latitude,
            longitude: e.globalData.longitude
        }, function(d) {
            t.request(e, "wx/cust/getOneQueueByShopId", "POST", {
                shopId: a,
                token: e.globalData.token,
                state: 1
            }, function(e) {
                e.data.state && i.setData({
                    addTime: e.data.data.timeStart,
                    nowIndex: e.data.data.nowIndex
                });
            }, function(e) {}), d.state ? i.setData({
                isQueue: !0,
                queueOrderId: d.data.queueOrderId
            }) : (i.setData({
                isQueue: !1
            }), wx.showToast({
                title: "排队失败,请重新操作",
                icon: "none"
            }));
        }, function(e) {
            wx.showToast({
                title: "排队失败",
                icon: "none"
            });
        });
    },
    cancel: function() {
        var a = this;
        t.request(e, "wx/cust/deletequeue", "POST", {
            token: e.globalData.token,
            queueOrderId: a.data.queueOrderId
        }, function(e) {
            e.data.state ? (wx.showToast({
                title: "取消成功"
            }), setTimeout(function() {
                wx.navigateBack();
            }, 1e3)) : wx.showToast({
                title: "取消失败",
                icon: "none"
            });
        }, function(e) {});
    },
    confirm: function() {
        var e = this.data.shopId;
        wx.navigateBack({
            url: "/pages/home/detail/detail?id=" + e
        });
    },
    yuyue: function() {
        wx.switchTab({
            url: "/pages/service/index"
        });
    },
    onShow: function() {},
    onUnload: function() {
        if (getCurrentPages().length > 3) {
            var e = getCurrentPages(), t = e[e.length - 3];
            t.data;
            t.setData({
                isBack: !0
            });
        }
    }
});